﻿// ------------------------------------------------------------------------
// 版权信息
// 版权归重庆虫儿飞科技有限公司所有。
// 所有权利保留。
// 官方网站：https://netokit.com
// 许可证信息
// Neto.Kit 项目主要遵循 MIT 许可证和 Apache 许可证（版本 2.0）进行分发和使用。
// 许可证的完整文本可以在源代码树根目录中的 LICENSE-APACHE 和 LICENSE-MIT 文件中找到。
// 
// 使用条款
// 使用本代码应遵守相关法律法规和许可证的要求。
// 
// 免责声明
// 对于因使用本代码而产生的任何直接、间接、偶然、特殊或后果性损害，我们不承担任何责任。
// 
// 其他重要信息
// Neto.Kit 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。
// 有关 Neto.Kit 项目的其他详细信息，请参阅位于源代码树根目录中的 COPYRIGHT 和 DISCLAIMER 文件。
// 
// 更多信息
// 请访问 https://netokit.com 获取更多关于 Neto.Kit 项目的许可证和版权信息。
// ------------------------------------------------------------------------

#region

using System.Data;
using DbType = SqlSugar.DbType;

#endregion

namespace Neto.Db.SqlSugar;

/// <summary>
///     sqlsugar扩展方法
/// </summary>
[SuppressSniffer]
public static class SqlSugarExtensions
{
    /// <summary>
    ///     更新忽略的字段
    /// </summary>
    private static readonly string[] UpdateIgnoreColumns = { "CreatorUserId", "CreatorUserName", "CreationTime" };

    #region 租户

    /// <summary>
    ///     转换为租户数据库
    /// </summary>
    /// <param name="db"></param>
    /// <returns></returns>
    public static ISqlSugarClient ToTenant(this ISqlSugarClient db)
    {
        //获取登录的用户信息
        //var tenantDbConfig = db.Queryable<dynamic>().First(o => o.TenantId == AppX.TenantId);
        var tenantDbConfig = db.Queryable<dynamic>().Where("TenantId=@TenantId", new { AppX.TenantId }).First();
        if (tenantDbConfig == null) return db;

        //验证当前上下文不存在则添加
        if (!db.AsTenant().IsAnyConnection(tenantDbConfig.ConfigId))
        {
            var conn = new ConnectionConfig
            {
                DbType = tenantDbConfig.DbType.ToEnum<DbType>(),
                ConfigId = tenantDbConfig.ConfigId, //设置库的唯一标识
                IsAutoCloseConnection = true,
                ConnectionString = tenantDbConfig.ConnString
            };

            db.AsTenant().AddConnection(conn);

            //注册sqlsugar对应的AOP
            var dbProvider = db.AsTenant().GetConnectionScope(tenantDbConfig.ConfigId);
            dbProvider.SetAop();
        }

        return db.AsTenant().GetConnectionScope(tenantDbConfig.ConfigId);
    }

    /// <summary>
    ///     转换为租户数据库
    /// </summary>
    /// <param name="db"></param>
    /// <param name="configId">配置ID</param>
    /// <returns></returns>
    public static ISqlSugarClient ToTenant(this ISqlSugarClient db, dynamic configId)
    {
        return db.AsTenant().GetConnectionScope(configId);
    }

    /// <summary>
    /// </summary>
    /// <param name="db"></param>
    /// <param name="connection"></param>
    public static void AddOrUpdateConnection(this ISqlSugarClient db, ConnectionConfig connection)
    {
        //如果存在，先移除，在新增
        if (db.AsTenant().IsAnyConnection(connection.ConfigId)) db.AsTenant().RemoveConnection(connection.ConfigId);
        db.AsTenant().AddConnection(connection);

        //注册sqlsugar对应的AOP
        var dbProvider = db.AsTenant().GetConnectionScope(connection.ConfigId);
        dbProvider.SetAop();

        ////从连接中移除
        //if (SqlSugarHelper.SqlSugarDbSetting.ConnectionConfigs.Any(o => o.ConfigId == connection.ConfigId))
        //{
        //    SqlSugarHelper.SqlSugarDbSetting.ConnectionConfigs.RemoveWhere(o => o.ConfigId == connection.ConfigId);
        //}
        ////新增连接
        //SqlSugarHelper.SqlSugarDbSetting.ConnectionConfigs.Add(connection);
    }

    #endregion 租户

    #region 查询数据-同步

    /// <summary>
    ///     根据ID查询一条数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="id"></param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <returns></returns>
    public static T GetById<T>(this ISqlSugarClient db, object id, bool isSplitTable = false)
    {
        if (isSplitTable) return db.Queryable<T>().In(id).SplitTable().First();
        return db.Queryable<T>().In(id).First();
    }

    /// <summary>
    ///     根据ID集合字符串查询多条数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="ids">ID集合，英文逗号隔开</param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <returns>返回List<T /></returns>
    public static List<T> GetByIds<T>(this ISqlSugarClient db, string ids, bool isSplitTable = false)
    {
        var objs = ids.ToLongList();
        if (isSplitTable) return db.Queryable<T>().In(objs).SplitTable().ToList();
        return db.Queryable<T>().In(objs).ToList();
    }

    /// <summary>
    ///     根据ID集合字符串查询多条数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="ids">ID集合，英文逗号隔开</param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <returns>返回List<T></returns>
    public static List<T> GetByIds<T>(this ISqlSugarClient db, object[] ids, bool isSplitTable = false)
    {
        if (isSplitTable) return db.Queryable<T>().In(ids).SplitTable().ToList();
        return db.Queryable<T>().In(ids).ToList();
    }

    /// <summary>
    ///     根据ID集合字符串查询多条数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="ids">ID集合，英文逗号隔开</param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <returns>返回List<T></returns>
    public static List<T> GetByIds<T>(this ISqlSugarClient db, List<object> ids, bool isSplitTable = false)
    {
        if (isSplitTable) return db.Queryable<T>().In(ids).SplitTable().ToList();
        return db.Queryable<T>().In(ids).ToList();
    }

    /// <summary>
    ///     获取全部数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <returns></returns>
    public static List<T> GetList<T>(this ISqlSugarClient db)
    {
        return db.Queryable<T>().ToList();
    }

    /// <summary>
    ///     获取列表
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression">条件表达式</param>
    /// <returns></returns>
    public static List<T> GetList<T>(this ISqlSugarClient db, Expression<Func<T, bool>> whereExpression)
    {
        return db.Queryable<T>().Where(whereExpression).ToList();
    }

    /// <summary>
    ///     获取单条数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression">条件表达式</param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <returns></returns>
    public static T GetSingle<T>(this ISqlSugarClient db, Expression<Func<T, bool>> whereExpression,
        bool isSplitTable = false)
    {
        if (isSplitTable) return db.Queryable<T>().Where(whereExpression).SplitTable().Single();
        return db.Queryable<T>().Single(whereExpression);
    }

    /// <summary>
    ///     获取第一条数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression">条件表达式</param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <returns></returns>
    public static T? GetFirst<T>(this ISqlSugarClient db, Expression<Func<T, bool>> whereExpression,
        bool isSplitTable = false)
        where T : class, new()
    {
        if (isSplitTable) return db.Queryable<T>().Where(whereExpression).SplitTable().First();
        return db.Queryable<T>().First(whereExpression);
    }

    /// <summary>
    ///     获取第一条数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression">条件表达式</param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <param name="orderBy">排序，默认:Id desc</param>
    /// <returns></returns>
    public static T? GetLast<T>(this ISqlSugarClient db, Expression<Func<T, bool>> whereExpression,
        bool isSplitTable = false, string orderBy = "Id desc")
        where T : class, new()
    {
        if (isSplitTable) return db.Queryable<T>().Where(whereExpression).SplitTable().OrderBy($"{orderBy}").First();
        return db.Queryable<T>().OrderBy($"{orderBy}").First(whereExpression);
    }

    /// <summary>
    ///     查询分页数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression">条件表达式</param>
    /// <param name="page">分页数据</param>
    /// <returns></returns>
    public static List<T> GetPageList<T>(this ISqlSugarClient db, Expression<Func<T, bool>> whereExpression,
        PageModel page)
        where T : class, new()
    {
        var count = 0;
        var result = db.Queryable<T>().Where(whereExpression).ToPageList(page.PageIndex, page.PageSize, ref count);
        page.TotalCount = count;
        return result;
    }

    /// <summary>
    ///     查询分页数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression">条件表达式</param>
    /// <param name="page">分页数据</param>
    /// <param name="orderByExpression">排序表达式</param>
    /// <param name="orderByType">排序方式，默认正序</param>
    /// <returns></returns>
    public static List<T> GetPageList<T>(this ISqlSugarClient db, Expression<Func<T, bool>> whereExpression,
        PageModel page, Expression<Func<T, object>> orderByExpression = null, OrderByType orderByType = OrderByType.Asc)
        where T : class, new()
    {
        var count = 0;
        var result = db.Queryable<T>().OrderByIF(orderByExpression != null, orderByExpression, orderByType)
            .Where(whereExpression).ToPageList(page.PageIndex, page.PageSize, ref count);
        page.TotalCount = count;
        return result;
    }

    /// <summary>
    ///     查询分页数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="conditionalList">条件集合</param>
    /// <param name="page">分页数据</param>
    /// <returns></returns>
    public static List<T> GetPageList<T>(this ISqlSugarClient db, List<IConditionalModel> conditionalList,
        PageModel page)
        where T : class, new()
    {
        var count = 0;
        var result = db.Queryable<T>().Where(conditionalList).ToPageList(page.PageIndex, page.PageSize, ref count);
        page.TotalCount = count;
        return result;
    }

    /// <summary>
    ///     查询分页数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="conditionalList">条件集合</param>
    /// <param name="page">分页数据</param>
    /// <param name="orderByExpression">排序表达式</param>
    /// <param name="orderByType">排序方式，默认正序</param>
    /// <returns></returns>
    public static List<T> GetPageList<T>(this ISqlSugarClient db, List<IConditionalModel> conditionalList,
        PageModel page, Expression<Func<T, object>> orderByExpression = null, OrderByType orderByType = OrderByType.Asc)
        where T : class, new()
    {
        var count = 0;
        var result = db.Queryable<T>().OrderByIF(orderByExpression != null, orderByExpression, orderByType)
            .Where(conditionalList).ToPageList(page.PageIndex, page.PageSize, ref count);
        page.TotalCount = count;
        return result;
    }

    #endregion 查询数据-同步

    #region 查询数据-异步

    /// <summary>
    ///     更加 主键查询
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="id">主键ID</param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <returns></returns>
    public static async Task<T> GetByIdAsync<T>(this ISqlSugarClient db, object id, bool isSplitTable = false)
        where T : class, new()
    {
        if (isSplitTable) return await db.Queryable<T>().In(id).SplitTable().FirstAsync();
        return await db.Queryable<T>().In(id).FirstAsync();
    }

    /// <summary>
    ///     根据ID集合字符串查询多条数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="ids">ID集合，英文逗号隔开</param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <returns>返回List<T></returns>
    public static async Task<List<T>> GetByIdsAsync<T>(this ISqlSugarClient db, string ids, bool isSplitTable = false)
        where T : class, new()
    {
        var objs = ids.ToLongList();
        if (isSplitTable) return await db.Queryable<T>().In(objs).SplitTable().ToListAsync();
        return await db.Queryable<T>().In(objs).ToListAsync();
    }

    /// <summary>
    ///     根据ID集合字符串查询多条数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="ids">ID集合，英文逗号隔开</param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <returns>返回List<T></returns>
    public static async Task<List<T>> GetByIdsAsync<T>(this ISqlSugarClient db, object[] ids,
        bool isSplitTable = false)
        where T : class, new()
    {
        if (isSplitTable) return await db.Queryable<T>().In(ids).SplitTable().ToListAsync();
        return await db.Queryable<T>().In(ids).ToListAsync();
    }

    /// <summary>
    ///     根据ID集合字符串查询多条数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="ids">ID集合，英文逗号隔开</param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <returns>返回List<T></returns>
    public static async Task<List<T>> GetByIdsAsync<T>(this ISqlSugarClient db, List<object> ids,
        bool isSplitTable = false)
        where T : class, new()
    {
        if (isSplitTable) return await db.Queryable<T>().In(ids).SplitTable().ToListAsync();
        return await db.Queryable<T>().In(ids).ToListAsync();
    }

    /// <summary>
    ///     获取全部数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <returns></returns>
    public static async Task<List<T>> GetListAsync<T>(this ISqlSugarClient db)
        where T : class, new()
    {
        return await db.Queryable<T>().ToListAsync();
    }

    /// <summary>
    ///     获取列表
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression">条件表达式</param>
    /// <returns></returns>
    public static async Task<List<T>> GetListAsync<T>(this ISqlSugarClient db,
        Expression<Func<T, bool>> whereExpression)
        where T : class, new()
    {
        return await db.Queryable<T>().Where(whereExpression).ToListAsync();
    }

    /// <summary>
    ///     查询唯一 一条数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression">条件表达式</param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <returns></returns>
    public static async Task<T> GetSingleAsync<T>(this ISqlSugarClient db, Expression<Func<T, bool>> whereExpression,
        bool isSplitTable = false)
        where T : class, new()
    {
        if (isSplitTable) return await db.Queryable<T>().Where(whereExpression).SplitTable().SingleAsync();
        return await db.Queryable<T>().SingleAsync(whereExpression);
    }

    /// <summary>
    ///     获取第一个数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression">条件表达式</param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <returns></returns>
    public static async Task<T?> GetFirstAsync<T>(this ISqlSugarClient db, Expression<Func<T, bool>> whereExpression,
        bool isSplitTable = false)
        where T : class, new()
    {
        if (isSplitTable) return await db.Queryable<T>().Where(whereExpression).SplitTable().FirstAsync();
        return await db.Queryable<T>().FirstAsync(whereExpression);
    }

    /// <summary>
    ///     获取第一个数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression">条件表达式</param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <param name="orderBy">排序，默认:Id desc</param>
    /// <returns></returns>
    public static async Task<T?> GetLastAsync<T>(this ISqlSugarClient db, Expression<Func<T, bool>> whereExpression,
        bool isSplitTable = false, string orderBy = "Id desc")
        where T : class, new()
    {
        if (isSplitTable)
            return await db.Queryable<T>().Where(whereExpression).SplitTable().OrderBy($"{orderBy}").FirstAsync();
        return await db.Queryable<T>().OrderBy($"{orderBy}").FirstAsync(whereExpression);
    }

    /// <summary>
    ///     查询分页数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression">条件表达式</param>
    /// <param name="page">分页信息</param>
    /// <returns></returns>
    public static async Task<List<T>> GetPageListAsync<T>(this ISqlSugarClient db,
        Expression<Func<T, bool>> whereExpression, PageModel page)
        where T : class, new()
    {
        RefAsync<int> count = 0;
        var result = await db.Queryable<T>().Where(whereExpression)
            .ToPageListAsync(page.PageIndex, page.PageSize, count);
        page.TotalCount = count;
        return result;
    }

    /// <summary>
    ///     查询分页数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression">条件表达式</param>
    /// <param name="page">分页信息</param>
    /// <param name="orderByExpression">排序表达式</param>
    /// <param name="orderByType">升降排序</param>
    /// <returns></returns>
    public static async Task<List<T>> GetPageListAsync<T>(this ISqlSugarClient db,
        Expression<Func<T, bool>> whereExpression, PageModel page, Expression<Func<T, object>> orderByExpression = null,
        OrderByType orderByType = OrderByType.Asc)
        where T : class, new()
    {
        RefAsync<int> count = 0;
        var result = await db.Queryable<T>().OrderByIF(orderByExpression != null, orderByExpression, orderByType)
            .Where(whereExpression).ToPageListAsync(page.PageIndex, page.PageSize, count);
        page.TotalCount = count;
        return result;
    }

    /// <summary>
    ///     查询分页数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="conditionalList">条件集合</param>
    /// <param name="page">分页信息</param>
    /// <returns></returns>
    public static async Task<List<T>> GetPageListAsync<T>(this ISqlSugarClient db,
        List<IConditionalModel> conditionalList, PageModel page)
        where T : class, new()
    {
        RefAsync<int> count = 0;
        var result = await db.Queryable<T>().Where(conditionalList)
            .ToPageListAsync(page.PageIndex, page.PageSize, count);
        page.TotalCount = count;
        return result;
    }

    /// <summary>
    ///     查询分页数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="conditionalList">条件集合</param>
    /// <param name="page">分页信息</param>
    /// <param name="orderByExpression">排序表达式</param>
    /// <param name="orderByType">升降排序</param>
    /// <returns></returns>
    public static async Task<List<T>> GetPageListAsync<T>(this ISqlSugarClient db,
        List<IConditionalModel> conditionalList, PageModel page, Expression<Func<T, object>> orderByExpression = null,
        OrderByType orderByType = OrderByType.Asc)
        where T : class, new()
    {
        RefAsync<int> count = 0;
        var result = await db.Queryable<T>().OrderByIF(orderByExpression != null, orderByExpression, orderByType)
            .Where(conditionalList).ToPageListAsync(page.PageIndex, page.PageSize, count);
        page.TotalCount = count;
        return result;
    }

    #endregion 查询数据-异步

    #region 查询数据-同步-映射转换为指定对象

    /// <summary>
    ///     根据ID查询一条数据
    /// </summary>
    /// <typeparam name="T">数据库实体</typeparam>
    /// <typeparam name="OutEntity">输出实体</typeparam>
    /// <param name="db"></param>
    /// <param name="id"></param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <returns></returns>
    public static OutEntity GetById<T, OutEntity>(this ISqlSugarClient db, object id, bool isSplitTable = false)
        where T : class, new()
    {
        if (!isSplitTable) return db.Queryable<T>().In(id).First().Adapt<OutEntity>();
        return db.Queryable<T>().In(id).SplitTable().First().Adapt<OutEntity>();
    }

    /// <summary>
    ///     获取全部数据
    /// </summary>
    /// <typeparam name="T">数据库实体</typeparam>
    /// <typeparam name="OutEntity">输出实体</typeparam>
    /// <param name="db"></param>
    /// <returns></returns>
    public static List<OutEntity> GetList<T, OutEntity>(this ISqlSugarClient db)
    {
        return db.Queryable<T>().ToList().Adapt<List<OutEntity>>();
    }

    /// <summary>
    ///     获取列表
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="OutEntity">输出实体</typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression">条件表达式</param>
    /// <returns></returns>
    public static List<OutEntity> GetList<T, OutEntity>(this ISqlSugarClient db,
        Expression<Func<T, bool>> whereExpression)
        where T : class, new()
    {
        return db.Queryable<T>().Where(whereExpression).ToList().Adapt<List<OutEntity>>();
    }

    /// <summary>
    ///     获取单条数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="OutEntity">输出实体</typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression">条件表达式</param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <returns></returns>
    public static OutEntity GetSingle<T, OutEntity>(this ISqlSugarClient db, Expression<Func<T, bool>> whereExpression,
        bool isSplitTable = false)
        where T : class, new()
    {
        if (!isSplitTable) return db.Queryable<T>().Single(whereExpression).Adapt<OutEntity>();
        return db.Queryable<T>().Where(whereExpression).SplitTable().Single().Adapt<OutEntity>();
    }

    /// <summary>
    ///     获取第一条数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="OutEntity">输出实体</typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression">条件表达式</param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <returns></returns>
    public static OutEntity? GetFirst<T, OutEntity>(this ISqlSugarClient db, Expression<Func<T, bool>> whereExpression,
        bool isSplitTable = false)
        where T : class, new()
    {
        if (!isSplitTable) return db.Queryable<T>().First(whereExpression).Adapt<OutEntity>();
        return db.Queryable<T>().Where(whereExpression).SplitTable().First().Adapt<OutEntity>();
    }


    /// <summary>
    ///     获取第一条数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="OutEntity">输出实体</typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression">条件表达式</param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <param name="orderBy">排序，默认:Id desc</param>
    /// <returns></returns>
    public static OutEntity? GetLast<T, OutEntity>(this ISqlSugarClient db, Expression<Func<T, bool>> whereExpression,
        bool isSplitTable = false, string orderBy = "Id desc")
        where T : class, new()
    {
        if (!isSplitTable) return db.Queryable<T>().OrderBy($"{orderBy}").First(whereExpression).Adapt<OutEntity>();
        return db.Queryable<T>().Where(whereExpression).SplitTable().OrderBy($"{orderBy}").First().Adapt<OutEntity>();
    }

    /// <summary>
    ///     查询分页数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="OutEntity">输出实体</typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression">条件表达式</param>
    /// <param name="page">分页数据</param>
    /// <returns></returns>
    public static List<OutEntity> GetPageList<T, OutEntity>(this ISqlSugarClient db,
        Expression<Func<T, bool>> whereExpression, PageModel page)
        where T : class, new()
    {
        var count = 0;
        var result = db.Queryable<T>().Where(whereExpression).ToPageList(page.PageIndex, page.PageSize, ref count);
        page.TotalCount = count;
        return result.Adapt<List<OutEntity>>();
    }

    /// <summary>
    ///     查询分页数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="OutEntity">输出实体</typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression">条件表达式</param>
    /// <param name="page">分页数据</param>
    /// <param name="orderByExpression">排序表达式</param>
    /// <param name="orderByType">排序方式，默认正序</param>
    /// <returns></returns>
    public static List<OutEntity> GetPageList<T, OutEntity>(this ISqlSugarClient db,
        Expression<Func<T, bool>> whereExpression, PageModel page, Expression<Func<T, object>> orderByExpression = null,
        OrderByType orderByType = OrderByType.Asc)
        where T : class, new()
    {
        var count = 0;
        var result = db.Queryable<T>().OrderByIF(orderByExpression != null, orderByExpression, orderByType)
            .Where(whereExpression).ToPageList(page.PageIndex, page.PageSize, ref count);
        page.TotalCount = count;
        return result.Adapt<List<OutEntity>>();
    }

    /// <summary>
    ///     查询分页数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="OutEntity">输出实体</typeparam>
    /// <param name="db"></param>
    /// <param name="conditionalList">条件集合</param>
    /// <param name="page">分页数据</param>
    /// <returns></returns>
    public static List<OutEntity> GetPageList<T, OutEntity>(this ISqlSugarClient db,
        List<IConditionalModel> conditionalList, PageModel page)
        where T : class, new()
    {
        var count = 0;
        var result = db.Queryable<T>().Where(conditionalList).ToPageList(page.PageIndex, page.PageSize, ref count);
        page.TotalCount = count;
        return result.Adapt<List<OutEntity>>();
    }

    /// <summary>
    ///     查询分页数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="OutEntity">输出实体</typeparam>
    /// <param name="db"></param>
    /// <param name="conditionalList">条件集合</param>
    /// <param name="page">分页数据</param>
    /// <param name="orderByExpression">排序表达式</param>
    /// <param name="orderByType">排序方式，默认正序</param>
    /// <returns></returns>
    public static List<OutEntity> GetPageList<T, OutEntity>(this ISqlSugarClient db,
        List<IConditionalModel> conditionalList, PageModel page, Expression<Func<T, object>> orderByExpression = null,
        OrderByType orderByType = OrderByType.Asc)
        where T : class, new()
    {
        var count = 0;
        var result = db.Queryable<T>().OrderByIF(orderByExpression != null, orderByExpression, orderByType)
            .Where(conditionalList).ToPageList(page.PageIndex, page.PageSize, ref count);
        page.TotalCount = count;
        return result.Adapt<List<OutEntity>>();
    }

    /// <summary>
    ///     分页查询所有数据
    ///     例如：查询订单表中的订单号、金额等字段。db.GetAllByPageAsync(o=>o.Id>0,o=>new TResult(){o.No,o.Price})
    ///     例如：查询订单表中的订单号字段，并且分页查询。db.GetAllByPageAsync(o=>o.Id>0,o=>o.No,pageSize:1000,pageIndex:1)
    /// </summary>
    /// <param name="db"></param>
    /// <param name="whereExpression">查询表达式</param>
    /// <param name="selectColumns">查询的列，用于不需要查询全部字段的情况</param>
    /// <param name="pageSize">每次查询条数，默认1000</param>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="TResult"></typeparam>
    /// <returns></returns>
    public static List<TResult> GetAllByPage<T, TResult>(this ISqlSugarClient db,
        Expression<Func<T, bool>> whereExpression = null, Expression<Func<T, TResult>> selectColumns = null,
        int pageSize = 1000)
        where T : class
        where TResult : class
    {
        var dataList = new List<TResult>();
        var pageIndex = 1;
        while (true)
        {
            var returnDbTotalCount = 0;
            List<TResult> resultData = null;

            var queryable = db.Queryable<T>();
            if (whereExpression != null) queryable = queryable.Where(whereExpression);
            if (selectColumns != null)
                resultData = queryable
                    .Select(selectColumns)
                    .ToPageList(pageIndex, pageSize, ref returnDbTotalCount);
            else
            {
                var resultData1 = queryable
                    .ToPageList(pageIndex, pageSize, ref returnDbTotalCount);
                resultData = resultData1.Adapt<List<TResult>>();
            }


            var totalPages = (int)Math.Ceiling(returnDbTotalCount / (double)pageSize);
            if (resultData.Count > 0)
            {
                dataList.AddRange(resultData);
                pageIndex++;
            }

            if (pageIndex >= totalPages)
                //没有数据了，跳出循环
                break;
        }

        return dataList;
    }

    #endregion 查询数据-同步-映射转换为指定对象

    #region 查询数据-异步-映射转换为指定对象

    /// <summary>
    ///     更加 主键查询
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="OutEntity">输出实体</typeparam>
    /// <param name="db"></param>
    /// <param name="id">主键ID</param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <returns></returns>
    public static async Task<OutEntity> GetByIdAsync<T, OutEntity>(this ISqlSugarClient db, object id,
        bool isSplitTable = false)
        where T : class, new()
    {
        if (isSplitTable)
        {
            var result = await db.Queryable<T>().In(id).SplitTable().FirstAsync();
            return result.Adapt<OutEntity>();
        }
        else
        {
            var result = await db.Queryable<T>().In(id).FirstAsync();
            return result.Adapt<OutEntity>();
        }
    }

    /// <summary>
    ///     获取全部数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="OutEntity">输出实体</typeparam>
    /// <param name="db"></param>
    /// <returns></returns>
    public static async Task<List<OutEntity>> GetListAsync<T, OutEntity>(this ISqlSugarClient db)
        where T : class, new()
    {
        var result = await db.Queryable<T>().ToListAsync();
        return result.Adapt<List<OutEntity>>();
    }

    /// <summary>
    ///     获取列表
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="OutEntity">输出实体</typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression">条件表达式</param>
    /// <returns></returns>
    public static async Task<List<OutEntity>> GetListAsync<T, OutEntity>(this ISqlSugarClient db,
        Expression<Func<T, bool>> whereExpression)
        where T : class, new()
    {
        var result = await db.Queryable<T>().Where(whereExpression).ToListAsync();
        return result.Adapt<List<OutEntity>>();
    }

    /// <summary>
    ///     查询唯一 一条数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="OutEntity">输出实体</typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression">条件表达式</param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <returns></returns>
    public static async Task<OutEntity> GetSingleAsync<T, OutEntity>(this ISqlSugarClient db,
        Expression<Func<T, bool>> whereExpression, bool isSplitTable = false)
        where T : class, new()
    {
        if (isSplitTable)
        {
            var result = await db.Queryable<T>().Where(whereExpression).SplitTable().SingleAsync();
            return result.Adapt<OutEntity>();
        }
        else
        {
            var result = await db.Queryable<T>().SingleAsync(whereExpression);
            return result.Adapt<OutEntity>();
        }
    }

    /// <summary>
    ///     获取第一个数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="OutEntity">输出实体</typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression">条件表达式</param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <returns></returns>
    public static async Task<OutEntity?> GetFirstAsync<T, OutEntity>(this ISqlSugarClient db,
        Expression<Func<T, bool>> whereExpression, bool isSplitTable = false)
        where T : class, new()
    {
        if (isSplitTable)
        {
            var result = await db.Queryable<T>().Where(whereExpression).SplitTable().FirstAsync();
            return result.Adapt<OutEntity>();
        }
        else
        {
            var result = await db.Queryable<T>().FirstAsync(whereExpression);
            return result.Adapt<OutEntity>();
        }
    }

    /// <summary>
    ///     获取第一个数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="OutEntity">输出实体</typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression">条件表达式</param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <param name="orderBy">排序，默认:Id desc</param>
    /// <returns></returns>
    public static async Task<OutEntity?> GetLastAsync<T, OutEntity>(this ISqlSugarClient db,
        Expression<Func<T, bool>> whereExpression, bool isSplitTable = false, string orderBy = "Id desc")
        where T : class, new()
    {
        if (isSplitTable)
        {
            var result = await db.Queryable<T>().Where(whereExpression).SplitTable().OrderBy($"{orderBy}").FirstAsync();
            return result.Adapt<OutEntity>();
        }
        else
        {
            var result = await db.Queryable<T>().OrderBy($"{orderBy}").FirstAsync(whereExpression);
            return result.Adapt<OutEntity>();
        }
    }

    /// <summary>
    ///     查询分页数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="OutEntity">输出实体</typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression">条件表达式</param>
    /// <param name="page">分页信息</param>
    /// <returns></returns>
    public static async Task<List<OutEntity>> GetPageListAsync<T, OutEntity>(this ISqlSugarClient db,
        Expression<Func<T, bool>> whereExpression, PageModel page)
        where T : class, new()
    {
        RefAsync<int> count = 0;
        var result = await db.Queryable<T>().Where(whereExpression)
            .ToPageListAsync(page.PageIndex, page.PageSize, count);
        page.TotalCount = count;
        return result.Adapt<List<OutEntity>>();
    }

    /// <summary>
    ///     查询分页数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="OutEntity">输出实体</typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression">条件表达式</param>
    /// <param name="page">分页信息</param>
    /// <param name="orderByExpression">排序表达式</param>
    /// <param name="orderByType">升降排序</param>
    /// <returns></returns>
    public static async Task<List<OutEntity>> GetPageListAsync<T, OutEntity>(this ISqlSugarClient db,
        Expression<Func<T, bool>> whereExpression, PageModel page, Expression<Func<T, object>> orderByExpression = null,
        OrderByType orderByType = OrderByType.Asc)
        where T : class, new()
    {
        RefAsync<int> count = 0;
        var result = await db.Queryable<T>().OrderByIF(orderByExpression != null, orderByExpression, orderByType)
            .Where(whereExpression).ToPageListAsync(page.PageIndex, page.PageSize, count);
        page.TotalCount = count;
        return result.Adapt<List<OutEntity>>();
    }

    /// <summary>
    ///     查询分页数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="OutEntity">输出实体</typeparam>
    /// <param name="db"></param>
    /// <param name="conditionalList">条件集合</param>
    /// <param name="page">分页信息</param>
    /// <returns></returns>
    public static async Task<List<OutEntity>> GetPageListAsync<T, OutEntity>(this ISqlSugarClient db,
        List<IConditionalModel> conditionalList, PageModel page)
        where T : class, new()
    {
        RefAsync<int> count = 0;
        var result = await db.Queryable<T>().Where(conditionalList)
            .ToPageListAsync(page.PageIndex, page.PageSize, count);
        page.TotalCount = count;
        return result.Adapt<List<OutEntity>>();
    }

    /// <summary>
    ///     查询分页数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="OutEntity">输出实体</typeparam>
    /// <param name="db"></param>
    /// <param name="conditionalList">条件集合</param>
    /// <param name="page">分页信息</param>
    /// <param name="orderByExpression">排序表达式</param>
    /// <param name="orderByType">升降排序</param>
    /// <returns></returns>
    public static async Task<List<OutEntity>> GetPageListAsync<T, OutEntity>(this ISqlSugarClient db,
        List<IConditionalModel> conditionalList, PageModel page, Expression<Func<T, object>> orderByExpression = null,
        OrderByType orderByType = OrderByType.Asc)
        where T : class, new()
    {
        RefAsync<int> count = 0;
        var result = await db.Queryable<T>().OrderByIF(orderByExpression != null, orderByExpression, orderByType)
            .Where(conditionalList).ToPageListAsync(page.PageIndex, page.PageSize, count);
        page.TotalCount = count;
        return result.Adapt<List<OutEntity>>();
    }

    /// <summary>
    ///     分页查询所有数据
    ///     例如：查询订单表中的订单号、金额等字段。db.GetAllByPageAsync(o=>o.Id>0,o=>new TResult(){o.No,o.Price})
    ///     例如：查询订单表中的订单号字段，并且分页查询。db.GetAllByPageAsync(o=>o.Id>0,o=>o.No,pageSize:1000,pageIndex:1)
    /// </summary>
    /// <param name="db"></param>
    /// <param name="whereExpression">查询表达式</param>
    /// <param name="selectColumns">查询的列，用于不需要查询全部字段的情况</param>
    /// <param name="pageSize">每次查询条数，默认1000</param>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="TResult"></typeparam>
    /// <returns></returns>
    public static async Task<List<TResult>> GetAllByPageAsync<T, TResult>(this ISqlSugarClient db,
        Expression<Func<T, bool>> whereExpression = null, Expression<Func<T, TResult>> selectColumns = null,
        int pageSize = 1000)
        where T : class
        where TResult : class
    {
        var dataList = new List<TResult>();
        var pageIndex = 1;
        while (true)
        {
            RefAsync<int> returnDbTotalCount = 0;
            List<TResult> resultData = null;

            var queryable = db.Queryable<T>();
            if (whereExpression != null) queryable = queryable.Where(whereExpression);

            if (selectColumns != null)
                resultData = await queryable
                    .Select(selectColumns)
                    .ToPageListAsync(pageIndex, pageSize, returnDbTotalCount);
            else
            {
                var resultData1 = await queryable
                    .ToPageListAsync(pageIndex, pageSize, returnDbTotalCount);
                resultData = resultData1.Adapt<List<TResult>>();
            }

            var totalPages = (int)Math.Ceiling(returnDbTotalCount / (double)pageSize);
            if (resultData.Count > 0)
            {
                dataList.AddRange(resultData);
                pageIndex++;
            }

            if (pageIndex >= totalPages)
                //没有数据了，跳出循环
                break;
        }

        return dataList;
    }

    #endregion 查询数据-异步-映射转换为指定对象

    #region 新增-同步

    /// <summary>
    ///     新增
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="insertObj">对象</param>
    /// <returns>返回bool类型</returns>
    public static bool Insert<T>(this ISqlSugarClient db, T insertObj) where T : class, new()
    {
        return db.Insertable(insertObj).ExecuteCommand() > 0;
    }

    /// <summary>
    ///     新增或更新
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="data">对象</param>
    /// <returns>返回bool类型</returns>
    public static bool InsertOrUpdate<T>(this ISqlSugarClient db, T data) where T : class, new()
    {
        return db.Storageable(data).ExecuteCommand() > 0;
    }

    /// <summary>
    ///     新增或更新集合
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="datas">对象</param>
    /// <returns>返回bool类型</returns>
    public static bool InsertOrUpdate<T>(this ISqlSugarClient db, List<T> datas) where T : class, new()
    {
        return db.Storageable(datas).ExecuteCommand() > 0;
    }

    /// <summary>
    ///     新增并返回自增ID
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="insertObj">对象</param>
    /// <returns>自增ID</returns>
    public static int InsertReturnIdentity<T>(this ISqlSugarClient db, T insertObj) where T : class, new()
    {
        return db.Insertable(insertObj).ExecuteReturnIdentity();
    }

    /// <summary>
    ///     新增并返回自增long ID
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="insertObj">对象</param>
    /// <returns>自增long ID</returns>
    public static long InsertReturnBigIdentity<T>(this ISqlSugarClient db, T insertObj) where T : class, new()
    {
        return db.Insertable(insertObj).ExecuteReturnBigIdentity();
    }

    /// <summary>
    ///     新增并返回雪花ID
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="insertObj">对象</param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <returns>雪花ID</returns>
    public static long InsertReturnSnowflakeId<T>(this ISqlSugarClient db, T insertObj, bool isSplitTable = false)
        where T : class, new()
    {
        if (isSplitTable) return db.Insertable(insertObj).SplitTable().ExecuteReturnSnowflakeId();
        return db.Insertable(insertObj).ExecuteReturnSnowflakeId();
    }

    /// <summary>
    ///     新增并返回雪花ID
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="insertObjs">集合</param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <returns>雪花ID集合</returns>
    public static List<long> InsertReturnSnowflakeId<T>(this ISqlSugarClient db, List<T> insertObjs,
        bool isSplitTable = false) where T : class, new()
    {
        if (isSplitTable) return db.Insertable(insertObjs).SplitTable().ExecuteReturnSnowflakeIdList();
        return db.Insertable(insertObjs).ExecuteReturnSnowflakeIdList();
    }

    /// <summary>
    ///     新增并返回数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="insertObj">对象</param>
    /// <returns>当前插入的数据</returns>
    public static T InsertReturnEntity<T>(this ISqlSugarClient db, T insertObj) where T : class, new()
    {
        return db.Insertable(insertObj).ExecuteReturnEntity();
    }

    /// <summary>
    ///     新增
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="insertObjs">数组</param>
    /// <returns>返回true或fasle</returns>
    public static bool Insert<T>(this ISqlSugarClient db, T[] insertObjs) where T : class, new()
    {
        return db.Insertable(insertObjs).ExecuteCommand() > 0;
    }

    /// <summary>
    ///     新增
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="insertObjs">数组</param>
    /// <returns>返回true或fasle</returns>
    public static bool Insert<T>(this ISqlSugarClient db, List<T> insertObjs) where T : class, new()
    {
        return db.Insertable(insertObjs).ExecuteCommand() > 0;
    }

    #endregion 新增-同步

    #region 新增-异步

    /// <summary>
    ///     新增或更新
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="data">对象</param>
    /// <returns></returns>
    public static async Task<bool> InsertOrUpdateAsync<T>(this ISqlSugarClient db, T data) where T : class, new()
    {
        return await db.Storageable(data).ExecuteCommandAsync() > 0;
    }

    /// <summary>
    ///     新增或更新
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="datas">集合</param>
    /// <returns></returns>
    public static async Task<bool> InsertOrUpdateAsync<T>(this ISqlSugarClient db, List<T> datas)
        where T : class, new()
    {
        return await db.Storageable(datas).ExecuteCommandAsync() > 0;
    }

    /// <summary>
    ///     新增
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="insertObj">对象</param>
    /// <returns></returns>
    public static async Task<bool> InsertAsync<T>(this ISqlSugarClient db, T insertObj) where T : class, new()
    {
        return await db.Insertable(insertObj).ExecuteCommandAsync() > 0;
    }

    /// <summary>
    ///     新增 -自增ID
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="insertObj">对象</param>
    /// <returns></returns>
    public static async Task<int> InsertReturnIdentityAsync<T>(this ISqlSugarClient db, T insertObj)
        where T : class, new()
    {
        return await db.Insertable(insertObj).ExecuteReturnIdentityAsync();
    }

    /// <summary>
    ///     新增-long自增ID
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="insertObj">对象</param>
    /// <returns></returns>
    public static async Task<long> InsertReturnBigIdentityAsync<T>(this ISqlSugarClient db, T insertObj)
        where T : class, new()
    {
        return await db.Insertable(insertObj).ExecuteReturnBigIdentityAsync();
    }

    /// <summary>
    ///     新增
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="insertObj">对象</param>
    /// <returns>新增实体</returns>
    public static async Task<T> InsertReturnEntityAsync<T>(this ISqlSugarClient db, T insertObj) where T : class, new()
    {
        return await db.Insertable(insertObj).ExecuteReturnEntityAsync();
    }

    /// <summary>
    ///     新增
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="insertObjs">数组</param>
    /// <returns></returns>
    public static async Task<bool> InsertAsync<T>(this ISqlSugarClient db, T[] insertObjs) where T : class, new()
    {
        return await db.Insertable(insertObjs).ExecuteCommandAsync() > 0;
    }

    /// <summary>
    ///     新增
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="insertObjs">集合</param>
    /// <returns></returns>
    public static async Task<bool> InsertAsync<T>(this ISqlSugarClient db, List<T> insertObjs) where T : class, new()
    {
        return await db.Insertable(insertObjs).ExecuteCommandAsync() > 0;
    }

    /// <summary>
    ///     新增并返回雪花ID
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="insertObj">对象</param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <returns>雪花ID</returns>
    public static Task<long> InsertReturnSnowflakeIdAsync<T>(this ISqlSugarClient db, T insertObj,
        bool isSplitTable = false) where T : class, new()
    {
        if (isSplitTable) return db.Insertable(insertObj).SplitTable().ExecuteReturnSnowflakeIdAsync();
        return db.Insertable(insertObj).ExecuteReturnSnowflakeIdAsync();
    }

    /// <summary>
    ///     新增并返回雪花ID
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="insertObjs">集合</param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <returns>雪花ID集合</returns>
    public static Task<List<long>> InsertReturnSnowflakeIdAsync<T>(this ISqlSugarClient db, List<T> insertObjs,
        bool isSplitTable = false) where T : class, new()
    {
        if (isSplitTable) return db.Insertable(insertObjs).SplitTable().ExecuteReturnSnowflakeIdListAsync();
        return db.Insertable(insertObjs).ExecuteReturnSnowflakeIdListAsync();
    }

    #endregion 新增-异步

    #region 表达式更新-同步

    /// <summary>
    ///     更新
    ///     【不追加更新过滤器赋值字段】
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="columns"></param>
    /// <param name="whereExpression"></param>
    /// <returns></returns>
    public static bool Update<T>(this ISqlSugarClient db, Expression<Func<T, T>> columns,
        Expression<Func<T, bool>> whereExpression) where T : class, new()
    {
        return db.Updateable<T>().SetColumns(columns).Where(whereExpression).ExecuteCommand() > 0;
    }

    /// <summary>
    ///     更新
    ///     【追加更新过滤器赋值字段】
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="columns"></param>
    /// <param name="whereExpression"></param>
    /// <returns></returns>
    public static bool UpdateSetColumnsTrue<T>(this ISqlSugarClient db, Expression<Func<T, T>> columns,
        Expression<Func<T, bool>> whereExpression) where T : class, new()
    {
        return db.Updateable<T>().SetColumns(columns, true).Where(whereExpression).ExecuteCommand() > 0;
    }

    /// <summary>
    ///     设置状态，bool类型的字段
    ///     例如：IsEnable 开关字段
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="id">主键的值</param>
    /// <param name="state">状态，bool类型</param>
    /// <param name="field">更新的字段名</param>
    /// <param name="primaryKeyField">组件字段，默认 id</param>
    /// <returns></returns>
    public static bool SetStateById<T>(this ISqlSugarClient db, dynamic id, bool state, string field,
        string primaryKeyField = "id") where T : class, new()
    {
        var dt = new Dictionary<string, object>();
        dt.Add(primaryKeyField, id);
        dt.Add(field, state); //需要更新的字段值
        return db.Updateable<T>(dt).WhereColumns("id").ExecuteCommand() > 0;
    }

    #endregion 表达式更新-同步

    #region 实体对象更新-同步

    /// <summary>
    ///     更新
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="updateObj">对象</param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <returns></returns>
    public static bool Update<T>(this ISqlSugarClient db, T updateObj, bool isSplitTable = false)
        where T : class, new()
    {
        if (isSplitTable)
            return db.Updateable(updateObj).IgnoreColumns(UpdateIgnoreColumns).SplitTable().ExecuteCommand() > 0;
        return db.Updateable(updateObj).IgnoreColumns(UpdateIgnoreColumns).ExecuteCommand() > 0;
    }

    /// <summary>
    ///     更新
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="updateObjs">数组</param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <returns></returns>
    public static bool Update<T>(this ISqlSugarClient db, T[] updateObjs, bool isSplitTable = false)
        where T : class, new()
    {
        if (isSplitTable)
            return db.Updateable(updateObjs).IgnoreColumns(UpdateIgnoreColumns).SplitTable().ExecuteCommand() > 0;
        return db.Updateable(updateObjs).IgnoreColumns(UpdateIgnoreColumns).ExecuteCommand() > 0;
    }

    /// <summary>
    ///     更新
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="updateObjs">集合</param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <returns></returns>
    public static bool Update<T>(this ISqlSugarClient db, List<T> updateObjs, bool isSplitTable = false)
        where T : class, new()
    {
        if (isSplitTable)
            return db.Updateable(updateObjs).IgnoreColumns(UpdateIgnoreColumns).SplitTable().ExecuteCommand() > 0;
        return db.Updateable(updateObjs).IgnoreColumns(UpdateIgnoreColumns).ExecuteCommand() > 0;
    }

    /// <summary>
    ///     更新
    ///     【不追加更新过滤器赋值字段】
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="obj">更新的对象</param>
    /// <param name="columns"></param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <returns></returns>
    public static bool Update<T>(this ISqlSugarClient db, T obj, Expression<Func<T, object>> columns,
        bool isSplitTable = false) where T : class, new()
    {
        if (isSplitTable) return db.Updateable(obj).UpdateColumns(columns).SplitTable().ExecuteCommand() > 0;
        return db.Updateable(obj).UpdateColumns(columns).ExecuteCommand() > 0;
    }

    /// <summary>
    ///     更新
    ///     【追加更新过滤器赋值字段】
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="obj">更新的对象</param>
    /// <param name="columns"></param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <returns></returns>
    public static bool UpdateSetColumnsTrue<T>(this ISqlSugarClient db, T obj, Expression<Func<T, object>> columns,
        bool isSplitTable = false) where T : class, new()
    {
        if (isSplitTable) return db.Updateable(obj).UpdateColumns(columns, true).SplitTable().ExecuteCommand() > 0;
        return db.Updateable(obj).UpdateColumns(columns, true).ExecuteCommand() > 0;
    }

    /// <summary>
    ///     更新
    ///     【追加更新过滤器赋值字段】
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="obj">更新的对象</param>
    /// <param name="columns">指定条件更新的列,例如：WhereColumns(it=>new { it.Id,it.Name}) //条件列不会被更新，只会作为条件</param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <returns></returns>
    public static bool UpdateByWhere<T>(this ISqlSugarClient db, T obj, Expression<Func<T, object>> columns,
        bool isSplitTable = false) where T : class, new()
    {
        if (isSplitTable) return db.Updateable(obj).WhereColumns(columns).SplitTable().ExecuteCommand() > 0;
        return db.Updateable(obj).WhereColumns(columns).ExecuteCommand() > 0;
    }

    #endregion 实体对象更新-同步

    #region 表达式更新-异步

    /// <summary>
    ///     更新
    ///     【不追加更新过滤器赋值字段】
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="columns">更新的列</param>
    /// <param name="whereExpression">条件表达式</param>
    /// <returns></returns>
    public static async Task<bool> UpdateAsync<T>(this ISqlSugarClient db, Expression<Func<T, T>> columns,
        Expression<Func<T, bool>> whereExpression) where T : class, new()
    {
        return await db.Updateable<T>().SetColumns(columns).Where(whereExpression).ExecuteCommandAsync() > 0;
    }

    /// <summary>
    ///     更新
    ///     【追加更新过滤器赋值字段】
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="columns">更新的列</param>
    /// <param name="whereExpression">条件表达式</param>
    /// <returns></returns>
    public static async Task<bool> UpdateSetColumnsTrueAsync<T>(this ISqlSugarClient db,
        Expression<Func<T, T>> columns, Expression<Func<T, bool>> whereExpression) where T : class, new()
    {
        return await db.Updateable<T>().SetColumns(columns, true).Where(whereExpression).ExecuteCommandAsync() > 0;
    }

    /// <summary>
    ///     设置状态，bool类型的字段
    ///     例如：IsEnable 开关字段
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="id">主键的值</param>
    /// <param name="state">状态，bool类型</param>
    /// <param name="field">更新的字段名</param>
    /// <param name="primaryKeyField">组件字段，默认 id</param>
    /// <returns></returns>
    public static async Task<bool> SetStateByIdAsync<T>(this ISqlSugarClient db, dynamic id, bool state, string field,
        string primaryKeyField = "id") where T : class, new()
    {
        var dt = new Dictionary<string, object>();
        dt.Add(primaryKeyField, id);
        dt.Add(field, state); //需要更新的字段值
        return await db.Updateable<T>(dt).WhereColumns("id").ExecuteCommandAsync() > 0;
    }

    #endregion 表达式更新-异步

    #region 实体更新-异步

    /// <summary>
    ///     更新
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="updateObj">对象</param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <returns></returns>
    public static async Task<bool> UpdateAsync<T>(this ISqlSugarClient db, T updateObj, bool isSplitTable = false)
        where T : class, new()
    {
        if (isSplitTable)
            return await db.Updateable(updateObj).IgnoreColumns(UpdateIgnoreColumns).SplitTable()
                .ExecuteCommandAsync() > 0;
        return await db.Updateable(updateObj).IgnoreColumns(UpdateIgnoreColumns).ExecuteCommandAsync() > 0;
    }

    /// <summary>
    ///     更新
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="updateObjs">数组</param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <returns></returns>
    public static async Task<bool> UpdateAsync<T>(this ISqlSugarClient db, T[] updateObjs, bool isSplitTable = false)
        where T : class, new()
    {
        if (isSplitTable)
            return await db.Updateable(updateObjs).IgnoreColumns(UpdateIgnoreColumns).SplitTable()
                .ExecuteCommandAsync() > 0;
        return await db.Updateable(updateObjs).IgnoreColumns(UpdateIgnoreColumns).ExecuteCommandAsync() > 0;
    }

    /// <summary>
    ///     更新
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="updateObjs">集合</param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <returns></returns>
    public static async Task<bool> UpdateAsync<T>(this ISqlSugarClient db, List<T> updateObjs,
        bool isSplitTable = false) where T : class, new()
    {
        if (isSplitTable)
            return await db.Updateable(updateObjs).IgnoreColumns(UpdateIgnoreColumns).SplitTable()
                .ExecuteCommandAsync() > 0;
        return await db.Updateable(updateObjs).IgnoreColumns(UpdateIgnoreColumns).ExecuteCommandAsync() > 0;
    }

    /// <summary>
    ///     更新
    ///     【不追加更新过滤器赋值字段】
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="obj">更新的对象</param>
    /// <param name="columns">更新的列</param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <returns></returns>
    public static async Task<bool> UpdateAsync<T>(this ISqlSugarClient db, T obj, Expression<Func<T, object>> columns,
        bool isSplitTable = false) where T : class, new()
    {
        if (isSplitTable) return await db.Updateable(obj).UpdateColumns(columns).SplitTable().ExecuteCommandAsync() > 0;
        return await db.Updateable(obj).UpdateColumns(columns).ExecuteCommandAsync() > 0;
    }

    /// <summary>
    ///     更新
    ///     【追加更新过滤器赋值字段】
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="obj">更新的对象</param>
    /// <param name="columns">更新的列</param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <returns></returns>
    public static async Task<bool> UpdateSetColumnsTrueAsync<T>(this ISqlSugarClient db, T obj,
        Expression<Func<T, object>> columns, bool isSplitTable = false) where T : class, new()
    {
        if (isSplitTable)
            return await db.Updateable(obj).UpdateColumns(columns, true).SplitTable().ExecuteCommandAsync() > 0;
        return await db.Updateable(obj).UpdateColumns(columns, true).ExecuteCommandAsync() > 0;
    }

    /// <summary>
    ///     更新
    ///     【追加更新过滤器赋值字段】
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="obj">更新的对象</param>
    /// <param name="columns">指定条件更新的列,例如：WhereColumns(it=>new { it.Id,it.Name}) //条件列不会被更新，只会作为条件</param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <returns></returns>
    public static async Task<bool> UpdateByWhereAsync<T>(this ISqlSugarClient db, T obj,
        Expression<Func<T, object>> columns, bool isSplitTable = false) where T : class, new()
    {
        if (isSplitTable) return await db.Updateable(obj).WhereColumns(columns).SplitTable().ExecuteCommandAsync() > 0;
        return await db.Updateable(obj).WhereColumns(columns).ExecuteCommandAsync() > 0;
    }

    #endregion 实体更新-异步

    #region 是否存在，数量

    /// <summary>
    ///     是否存在
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression">条件表达式</param>
    /// <returns></returns>
    public static bool IsAny<T>(this ISqlSugarClient db, Expression<Func<T, bool>> whereExpression)
    {
        return db.Queryable<T>().Where(whereExpression).Any();
    }

    /// <summary>
    ///     获取数量
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression">条件表达式</param>
    /// <returns></returns>
    public static int Count<T>(this ISqlSugarClient db, Expression<Func<T, bool>> whereExpression)
    {
        return db.Queryable<T>().Where(whereExpression).Count();
    }

    /// <summary>
    ///     是否存在
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression"></param>
    /// <returns></returns>
    public static async Task<bool> IsAnyAsync<T>(this ISqlSugarClient db, Expression<Func<T, bool>> whereExpression)
    {
        return await db.Queryable<T>().Where(whereExpression).AnyAsync();
    }

    /// <summary>
    ///     数量
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression"></param>
    /// <returns></returns>
    public static async Task<int> CountAsync<T>(this ISqlSugarClient db, Expression<Func<T, bool>> whereExpression)
    {
        return await db.Queryable<T>().Where(whereExpression).CountAsync();
    }

    #endregion 是否存在，数量

    #region 删除-同步

    /// <summary>
    ///     删除
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="deleteObj">对象</param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <returns></returns>
    public static bool Delete<T>(this ISqlSugarClient db, T deleteObj, bool isSplitTable = false)
        where T : class, new()
    {
        if (isSplitTable) return db.Deleteable(deleteObj).SplitTable().ExecuteCommand() > 0;
        return db.Deleteable(deleteObj).ExecuteCommand() > 0;
    }

    /// <summary>
    ///     删除
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="deleteObjs">集合</param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <returns></returns>
    public static bool Delete<T>(this ISqlSugarClient db, List<T> deleteObjs, bool isSplitTable = false)
        where T : class, new()
    {
        if (isSplitTable) return db.Deleteable(deleteObjs).SplitTable().ExecuteCommand() > 0;
        return db.Deleteable(deleteObjs).ExecuteCommand() > 0;
    }

    /// <summary>
    ///     删除
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression">条件表达式</param>
    /// <returns></returns>
    public static bool Delete<T>(this ISqlSugarClient db, Expression<Func<T, bool>> whereExpression)
        where T : class, new()
    {
        return db.Deleteable<T>().Where(whereExpression).ExecuteCommand() > 0;
    }

    /// <summary>
    ///     更加主键删除数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="id"></param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <returns></returns>
    public static bool DeleteById<T>(this ISqlSugarClient db, object id, bool isSplitTable = false)
        where T : class, new()
    {
        if (!isSplitTable) return db.Deleteable<T>().In(id).ExecuteCommand() > 0;
        var datas = db.GetById<T>(id, isSplitTable);
        return db.Delete(datas, isSplitTable);
    }

    /// <summary>
    ///     根据数组删除数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="ids"></param>
    /// <param name="isSplitTable"></param>
    /// <returns></returns>
    public static bool DeleteByIds<T>(this ISqlSugarClient db, object[] ids, bool isSplitTable = false)
        where T : class, new()
    {
        if (!isSplitTable) return db.Deleteable<T>().In(ids).ExecuteCommand() > 0;
        var datas = db.GetByIds<T>(ids, isSplitTable);
        return db.Delete(datas, isSplitTable);
    }

    /// <summary>
    ///     根据数组删除数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="ids"></param>
    /// <param name="isSplitTable"></param>
    /// <returns></returns>
    public static bool DeleteByIds<T>(this ISqlSugarClient db, List<object> ids, bool isSplitTable = false)
        where T : class, new()
    {
        if (!isSplitTable) return db.Deleteable<T>().In(ids).ExecuteCommand() > 0;
        var datas = db.GetByIds<T>(ids.ToArray(), isSplitTable);
        return db.Delete(datas, isSplitTable);
    }

    /// <summary>
    ///     删除
    /// </summary>
    /// <param name="db"></param>
    /// <param name="ids">id字符串，英文逗号隔开</param>
    /// <param name="isSplitTable"></param>
    /// <returns></returns>
    public static bool DeleteByIdStr<T>(this ISqlSugarClient db, string ids, bool isSplitTable = false)
        where T : class, new()
    {
        var objs = ids.ToSplitObjects();
        if (!isSplitTable) return db.Deleteable<T>().In(objs).ExecuteCommand() > 0;
        var datas = db.GetByIds<T>(ids, isSplitTable);
        return db.Delete(datas, isSplitTable);
    }

    #endregion 删除-同步

    #region 删除-异步

    /// <summary>
    ///     删除
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="deleteObj">对象</param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <returns></returns>
    public static async Task<bool> DeleteAsync<T>(this ISqlSugarClient db, T deleteObj, bool isSplitTable = false)
        where T : class, new()
    {
        if (isSplitTable) return await db.Deleteable(deleteObj).SplitTable().ExecuteCommandAsync() > 0;
        return await db.Deleteable(deleteObj).ExecuteCommandAsync() > 0;
    }

    /// <summary>
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="deleteObjs">集合</param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <returns></returns>
    public static async Task<bool> DeleteAsync<T>(this ISqlSugarClient db, List<T> deleteObjs,
        bool isSplitTable = false) where T : class, new()
    {
        if (isSplitTable) return await db.Deleteable(deleteObjs).SplitTable().ExecuteCommandAsync() > 0;
        return await db.Deleteable(deleteObjs).ExecuteCommandAsync() > 0;
    }

    /// <summary>
    ///     删除
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression">条件表达式</param>
    /// <returns></returns>
    public static async Task<bool> DeleteAsync<T>(this ISqlSugarClient db, Expression<Func<T, bool>> whereExpression)
        where T : class, new()
    {
        return await db.Deleteable<T>().Where(whereExpression).ExecuteCommandAsync() > 0;
    }

    /// <summary>
    ///     更加主键删除
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="id">主键ID</param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <returns></returns>
    public static async Task<bool> DeleteByIdAsync<T>(this ISqlSugarClient db, object id, bool isSplitTable = false)
        where T : class, new()
    {
        if (!isSplitTable) return await db.Deleteable<T>().In(id).ExecuteCommandAsync() > 0;
        var data = await db.GetByIdAsync<T>(id, isSplitTable);
        return await db.DeleteAsync(data, isSplitTable);
    }

    /// <summary>
    ///     删除
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="ids">主键集合</param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <returns></returns>
    public static async Task<bool> DeleteByIdsAsync<T>(this ISqlSugarClient db, object[] ids,
        bool isSplitTable = false) where T : class, new()
    {
        if (!isSplitTable) return await db.Deleteable<T>().In(ids).ExecuteCommandAsync() > 0;
        var data = await db.GetByIdAsync<T>(ids, isSplitTable);
        return await db.DeleteAsync(data, isSplitTable);
    }

    /// <summary>
    ///     删除
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="ids">主键集合</param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <returns></returns>
    public static async Task<bool> DeleteByIdsAsync<T>(this ISqlSugarClient db, List<object> ids,
        bool isSplitTable = false) where T : class, new()
    {
        if (!isSplitTable) return await db.Deleteable<T>().In(ids).ExecuteCommandAsync() > 0;
        var data = await db.GetByIdAsync<T>(ids, isSplitTable);
        return await db.DeleteAsync(data, isSplitTable);
    }

    /// <summary>
    ///     删除
    /// </summary>
    /// <param name="db"></param>
    /// <param name="ids">id字符串，英文逗号隔开</param>
    /// <param name="isSplitTable"></param>
    /// <returns></returns>
    public static async Task<bool> DeleteByIdsAsync<T>(this ISqlSugarClient db, string ids, bool isSplitTable = false)
        where T : class, new()
    {
        var objs = ids.ToSplitObjects();
        if (!isSplitTable) return await db.Deleteable<T>().In(objs).ExecuteCommandAsync() > 0;
        var datas = await db.GetByIdsAsync<T>(ids, isSplitTable);
        return await db.DeleteAsync(datas, true);
    }

    #endregion 删除-异步

    #region 假删除

    #region FakeDelete

    /// <summary>
    ///     假删除
    /// </summary>
    /// <typeparam name="T">实体</typeparam>
    /// <param name="db"></param>
    /// <param name="ids">删除的id字符串，英文逗号隔开</param>
    /// <param name="isSplitTable"></param>
    /// <returns></returns>
    public static bool FakeDelete<T>(this ISqlSugarClient db, string ids, bool isSplitTable = false)
        where T : class, IDeleted<long>, new()
    {
        return db.FakeDelete<T, long>(ids, isSplitTable);
    }

    /// <summary>
    ///     假删除
    /// </summary>
    /// <typeparam name="T">实体</typeparam>
    /// <param name="db"></param>
    /// <param name="ids">删除的id字符串，英文逗号隔开</param>
    /// <param name="isSplitTable"></param>
    /// <returns></returns>
    public static bool FakeDeleteByIdIsInt<T>(this ISqlSugarClient db, string ids, bool isSplitTable = false)
        where T : class, IDeleted<int>, new()
    {
        return db.FakeDelete<T, int>(ids, isSplitTable);
    }

    /// <summary>
    ///     假删除
    /// </summary>
    /// <typeparam name="T">实体</typeparam>
    /// <param name="db"></param>
    /// <param name="ids">删除的id字符串，英文逗号隔开</param>
    /// <param name="isSplitTable"></param>
    /// <returns></returns>
    public static bool FakeDeleteByIdIsString<T>(this ISqlSugarClient db, string ids, bool isSplitTable = false)
        where T : class, IDeleted<string>, new()
    {
        return db.FakeDelete<T, string>(ids, isSplitTable);
    }

    /// <summary>
    ///     假删除
    /// </summary>
    /// <typeparam name="T">实体</typeparam>
    /// <typeparam name="TIdType">默认long</typeparam>
    /// <param name="db"></param>
    /// <param name="ids">删除的id字符串，英文逗号隔开</param>
    /// <param name="isSplitTable"></param>
    /// <returns></returns>
    public static bool FakeDelete<T, TIdType>(this ISqlSugarClient db, string ids, bool isSplitTable = false)
        where T : class, IDeleted<TIdType>, new()
    {
        var objs = ids.ToSplitObjects();
        if (isSplitTable)
        {
            //查询出实体
            var datas = db.GetByIds<T>(ids, isSplitTable);
            //更加实体更新，会自动更加日期查找表
            return db.Updateable(datas)
                .SetColumns(
                    it => new T
                    {
                        IsDeleted = true,
                        DeletedTime = DateTimeOffset.Now,
                        DeletedUserId = App.HttpContext.GetLoginUserId<TIdType>(),
                        DeletedUserName = App.HttpContext.GetLoginUserAccount()
                    },
                    true) //true 支持更新数据过滤器赋值字段一起更新
                //.Where(exp)
                .SplitTable()
                .ExecuteCommand() > 0;
        }

        return db.Updateable<T>()
            .SetColumns(
                it => new T
                {
                    IsDeleted = true,
                    DeletedTime = DateTimeOffset.Now,
                    DeletedUserId = App.HttpContext.GetLoginUserId<TIdType>(),
                    DeletedUserName = App.HttpContext.GetLoginUserAccount()
                },
                true) //true 支持更新数据过滤器赋值字段一起更新
            //.Where(exp)
            .In(objs)
            .ExecuteCommand() > 0;
    }

    #endregion

    #region FakeDeleteAsync

    /// <summary>
    ///     假删除
    /// </summary>
    /// <typeparam name="T">实体</typeparam>
    /// <param name="db"></param>
    /// <param name="ids">删除的id字符串，英文逗号隔开</param>
    /// <param name="isSplitTable"></param>
    /// <returns></returns>
    public static async Task<bool> FakeDeleteAsync<T>(this ISqlSugarClient db, string ids,
        bool isSplitTable = false)
        where T : class, IDeleted<long>, new()
    {
        return await db.FakeDeleteAsync<T, long>(ids, isSplitTable);
    }

    /// <summary>
    ///     假删除
    /// </summary>
    /// <typeparam name="T">实体</typeparam>
    /// <param name="db"></param>
    /// <param name="ids">删除的id字符串，英文逗号隔开</param>
    /// <param name="isSplitTable"></param>
    /// <returns></returns>
    public static async Task<bool> FakeDeleteByIdIsIntAsync<T>(this ISqlSugarClient db, string ids,
        bool isSplitTable = false)
        where T : class, IDeleted<int>, new()
    {
        return await db.FakeDeleteAsync<T, int>(ids, isSplitTable);
    }

    /// <summary>
    ///     假删除
    /// </summary>
    /// <typeparam name="T">实体</typeparam>
    /// <param name="db"></param>
    /// <param name="ids">删除的id字符串，英文逗号隔开</param>
    /// <param name="isSplitTable"></param>
    /// <returns></returns>
    public static async Task<bool> FakeDeleteByIdIsStringAsync<T>(this ISqlSugarClient db, string ids,
        bool isSplitTable = false)
        where T : class, IDeleted<string>, new()
    {
        return await db.FakeDeleteAsync<T, string>(ids, isSplitTable);
    }


    /// <summary>
    ///     假删除
    /// </summary>
    /// <typeparam name="T">实体</typeparam>
    /// <typeparam name="TIdType"></typeparam>
    /// <param name="db"></param>
    /// <param name="ids">删除的id字符串，英文逗号隔开</param>
    /// <param name="isSplitTable"></param>
    /// <returns></returns>
    public static async Task<bool> FakeDeleteAsync<T, TIdType>(this ISqlSugarClient db, string ids,
        bool isSplitTable = false)
        where T : class, IDeleted<TIdType>, new()
    {
        var objs = ids.ToSplitObjects();
        if (isSplitTable)
        {
            //查询出实体
            var datas = await db.GetByIdsAsync<T>(ids, isSplitTable);
            //更加实体更新，会自动更加日期查找表
            return await db.Updateable(datas)
                .SetColumns(
                    it => new T
                    {
                        IsDeleted = true,
                        DeletedTime = DateTimeOffset.Now,
                        DeletedUserId = App.HttpContext.GetLoginUserId<TIdType>(),
                        DeletedUserName = App.HttpContext.GetLoginUserAccount()
                    },
                    true) //true 支持更新数据过滤器赋值字段一起更新
                .ExecuteCommandAsync() > 0;
        }

        return await db.Updateable<T>()
            .SetColumns(
                it => new T
                {
                    IsDeleted = true,
                    DeletedTime = DateTimeOffset.Now,
                    DeletedUserId = App.HttpContext.GetLoginUserId<TIdType>(),
                    DeletedUserName = App.HttpContext.GetLoginUserAccount()
                },
                true) //true 支持更新数据过滤器赋值字段一起更新
            .In(objs)
            .ExecuteCommandAsync() > 0;
    }

    #endregion

    #region FakeDelete

    /// <summary>
    ///     假删除
    /// </summary>
    /// <typeparam name="T">实体</typeparam>
    /// <param name="db"></param>
    /// <param name="ids">删除的id字符串，英文逗号隔开</param>
    /// <param name="isSplitTable"></param>
    /// <returns></returns>
    public static bool FakeDelete<T>(this ISqlSugarClient db, List<object> ids, bool isSplitTable = false)
        where T : class, IDeleted<long>, new()
    {
        return db.FakeDelete<T, long>(ids, isSplitTable);
    }

    /// <summary>
    ///     假删除
    /// </summary>
    /// <typeparam name="T">实体</typeparam>
    /// <param name="db"></param>
    /// <param name="ids">删除的id字符串，英文逗号隔开</param>
    /// <param name="isSplitTable"></param>
    /// <returns></returns>
    public static bool FakeDeleteByIdIsInt<T>(this ISqlSugarClient db, List<object> ids, bool isSplitTable = false)
        where T : class, IDeleted<int>, new()
    {
        return db.FakeDelete<T, int>(ids, isSplitTable);
    }

    /// <summary>
    ///     假删除
    /// </summary>
    /// <typeparam name="T">实体</typeparam>
    /// <param name="db"></param>
    /// <param name="ids">删除的id字符串，英文逗号隔开</param>
    /// <param name="isSplitTable"></param>
    /// <returns></returns>
    public static bool FakeDeleteByIdIsString<T>(this ISqlSugarClient db, List<object> ids, bool isSplitTable = false)
        where T : class, IDeleted<string>, new()
    {
        return db.FakeDelete<T, string>(ids, isSplitTable);
    }

    /// <summary>
    ///     假删除
    /// </summary>
    /// <typeparam name="T">实体</typeparam>
    /// <typeparam name="TIdType"></typeparam>
    /// <param name="db"></param>
    /// <param name="ids">删除的id字符串，英文逗号隔开</param>
    /// <param name="isSplitTable"></param>
    /// <returns></returns>
    public static bool FakeDelete<T, TIdType>(this ISqlSugarClient db, List<object> ids, bool isSplitTable = false)
        where T : class, IDeleted<TIdType>, new()
    {
        if (isSplitTable)
        {
            //查询出实体
            var datas = db.GetByIds<T>(ids, isSplitTable);
            //更加实体更新，会自动更加日期查找表
            return db.Updateable(datas)
                .SetColumns(
                    it => new T
                    {
                        IsDeleted = true,
                        DeletedTime = DateTimeOffset.Now,
                        DeletedUserId = App.HttpContext.GetLoginUserId<TIdType>(),
                        DeletedUserName = App.HttpContext.GetLoginUserAccount()
                    },
                    true) //true 支持更新数据过滤器赋值字段一起更新
                //.Where(exp)
                .SplitTable()
                .ExecuteCommand() > 0;
        }

        return db.Updateable<T>()
            .SetColumns(
                it => new T
                {
                    IsDeleted = true,
                    DeletedTime = DateTimeOffset.Now,
                    DeletedUserId = App.HttpContext.GetLoginUserId<TIdType>(),
                    DeletedUserName = App.HttpContext.GetLoginUserAccount()
                },
                true) //true 支持更新数据过滤器赋值字段一起更新
            //.Where(exp)
            .In(ids.ToArray())
            .ExecuteCommand() > 0;
    }

    #endregion

    #region FakeDeleteAsync

    /// <summary>
    ///     假删除
    /// </summary>
    /// <typeparam name="T">实体</typeparam>
    /// <param name="db"></param>
    /// <param name="ids">删除的id字符串，英文逗号隔开</param>
    /// <param name="isSplitTable"></param>
    /// <returns></returns>
    public static async Task<bool> FakeDeleteAsync<T>(this ISqlSugarClient db, List<object> ids,
        bool isSplitTable = false) where T : class, IDeleted<long>, new()
    {
        return await db.FakeDeleteAsync<T, long>(ids, isSplitTable);
    }

    /// <summary>
    ///     假删除
    /// </summary>
    /// <typeparam name="T">实体</typeparam>
    /// <param name="db"></param>
    /// <param name="ids">删除的id字符串，英文逗号隔开</param>
    /// <param name="isSplitTable"></param>
    /// <returns></returns>
    public static async Task<bool> FakeDeleteByIdIsIntAsync<T>(this ISqlSugarClient db, List<object> ids,
        bool isSplitTable = false) where T : class, IDeleted<int>, new()
    {
        return await db.FakeDeleteAsync<T, int>(ids, isSplitTable);
    }

    /// <summary>
    ///     假删除
    /// </summary>
    /// <typeparam name="T">实体</typeparam>
    /// <param name="db"></param>
    /// <param name="ids">删除的id字符串，英文逗号隔开</param>
    /// <param name="isSplitTable"></param>
    /// <returns></returns>
    public static async Task<bool> FakeDeleteByIdIsStringAsync<T>(this ISqlSugarClient db, List<object> ids,
        bool isSplitTable = false) where T : class, IDeleted<string>, new()
    {
        return await db.FakeDeleteAsync<T, string>(ids, isSplitTable);
    }

    /// <summary>
    ///     假删除
    /// </summary>
    /// <typeparam name="T">实体</typeparam>
    /// <typeparam name="TIdType"></typeparam>
    /// <param name="db"></param>
    /// <param name="ids">删除的id字符串，英文逗号隔开</param>
    /// <param name="isSplitTable"></param>
    /// <returns></returns>
    public static async Task<bool> FakeDeleteAsync<T, TIdType>(this ISqlSugarClient db, List<object> ids,
        bool isSplitTable = false) where T : class, IDeleted<TIdType>, new()
    {
        if (isSplitTable)
        {
            //查询出实体
            var datas = await db.GetByIdsAsync<T>(ids, isSplitTable);
            //更加实体更新，会自动更加日期查找表
            return await db.Updateable(datas)
                .SetColumns(
                    it => new T
                    {
                        IsDeleted = true,
                        DeletedTime = DateTimeOffset.Now,
                        DeletedUserId = App.HttpContext.GetLoginUserId<TIdType>(),
                        DeletedUserName = App.HttpContext.GetLoginUserAccount()
                    },
                    true) //true 支持更新数据过滤器赋值字段一起更新
                .ExecuteCommandAsync() > 0;
        }

        return await db.Updateable<T>()
            .SetColumns(
                it => new T
                {
                    IsDeleted = true,
                    DeletedTime = DateTimeOffset.Now,
                    DeletedUserId = App.HttpContext.GetLoginUserId<TIdType>(),
                    DeletedUserName = App.HttpContext.GetLoginUserAccount()
                },
                true) //true 支持更新数据过滤器赋值字段一起更新
            .In(ids.ToArray())
            .ExecuteCommandAsync() > 0;
    }

    #endregion

    #region FakeDelete

    /// <summary>
    ///     假删除
    /// </summary>
    /// <typeparam name="T">实体</typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression">条件表达式</param>
    /// <returns></returns>
    public static bool FakeDelete<T>(this ISqlSugarClient db, Expression<Func<T, bool>> whereExpression)
        where T : class, IDeleted<long>, new()
    {
        return db.FakeDelete<T, long>(whereExpression);
    }

    /// <summary>
    ///     假删除
    /// </summary>
    /// <typeparam name="T">实体</typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression">条件表达式</param>
    /// <returns></returns>
    public static bool FakeDeleteByIdIsInt<T>(this ISqlSugarClient db, Expression<Func<T, bool>> whereExpression)
        where T : class, IDeleted<int>, new()
    {
        return db.FakeDelete<T, int>(whereExpression);
    }

    /// <summary>
    ///     假删除
    /// </summary>
    /// <typeparam name="T">实体</typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression">条件表达式</param>
    /// <returns></returns>
    public static bool FakeDeleteByIdIsString<T>(this ISqlSugarClient db, Expression<Func<T, bool>> whereExpression)
        where T : class, IDeleted<string>, new()
    {
        return db.FakeDelete<T, string>(whereExpression);
    }

    /// <summary>
    ///     假删除
    /// </summary>
    /// <typeparam name="T">实体</typeparam>
    /// <typeparam name="TIdType"></typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression">条件表达式</param>
    /// <returns></returns>
    public static bool FakeDelete<T, TIdType>(this ISqlSugarClient db, Expression<Func<T, bool>> whereExpression)
        where T : class, IDeleted<TIdType>, new()
    {
        //return db.Deleteable<T>().Where(whereExpression)
        //    .IsLogic()
        //    .ExecuteCommand("IsDeleted", true, "DeletedTime", "DeletedUserName", App.HttpContext.GetLoginUserAccount()) > 0;

        return db.Updateable<T>()
            .SetColumns(
                it => new T
                {
                    IsDeleted = true,
                    DeletedTime = DateTimeOffset.Now,
                    DeletedUserId = App.HttpContext.GetLoginUserId<TIdType>(),
                    DeletedUserName = App.HttpContext.GetLoginUserAccount()
                },
                true) //true 支持更新数据过滤器赋值字段一起更新
            .Where(whereExpression)
            .ExecuteCommand() > 0;
    }

    #endregion


    #region FakeDeleteAsync

    /// <summary>
    ///     假删除
    /// </summary>
    /// <typeparam name="T">实体</typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression">条件表达式</param>
    /// <returns></returns>
    public static async Task<bool> FakeDeleteAsync<T>(this ISqlSugarClient db,
        Expression<Func<T, bool>> whereExpression) where T : class, IDeleted<long>, new()
    {
        return await db.FakeDeleteAsync<T, long>(whereExpression);
    }

    /// <summary>
    ///     假删除
    /// </summary>
    /// <typeparam name="T">实体</typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression">条件表达式</param>
    /// <returns></returns>
    public static async Task<bool> FakeDeleteByIdIsIntAsync<T>(this ISqlSugarClient db,
        Expression<Func<T, bool>> whereExpression) where T : class, IDeleted<int>, new()
    {
        return await db.FakeDeleteAsync<T, int>(whereExpression);
    }

    /// <summary>
    ///     假删除
    /// </summary>
    /// <typeparam name="T">实体</typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression">条件表达式</param>
    /// <returns></returns>
    public static async Task<bool> FakeDeleteByIdIsStringAsync<T>(this ISqlSugarClient db,
        Expression<Func<T, bool>> whereExpression) where T : class, IDeleted<string>, new()
    {
        return await db.FakeDeleteAsync<T, string>(whereExpression);
    }


    /// <summary>
    ///     假删除
    /// </summary>
    /// <typeparam name="T">实体</typeparam>
    /// <typeparam name="TIdType"></typeparam>
    /// <param name="db"></param>
    /// <param name="whereExpression">条件表达式</param>
    /// <returns></returns>
    public static async Task<bool> FakeDeleteAsync<T, TIdType>(this ISqlSugarClient db,
        Expression<Func<T, bool>> whereExpression) where T : class, IDeleted<TIdType>, new()
    {
        //return await db.Deleteable<T>().Where(whereExpression)
        //     .IsLogic()
        //     .ExecuteCommandAsync("IsDeleted", true, "DeletedTime", "DeletedUserName", App.HttpContext.GetLoginUserAccount()) > 0;
        return await db.Updateable<T>()
            .SetColumns(
                it => new T
                {
                    IsDeleted = true,
                    DeletedTime = DateTimeOffset.Now,
                    DeletedUserId = App.HttpContext.GetLoginUserId<TIdType>(),
                    DeletedUserName = App.HttpContext.GetLoginUserAccount()
                },
                true) //true 支持更新数据过滤器赋值字段一起更新
            .Where(whereExpression)
            .ExecuteCommandAsync() > 0;
    }

    #endregion

    #endregion 假删除

    #region 新增简写扩展-同步

    /// <summary>
    ///     新增并返回自增ID
    ///     【InsertReturnIdentity】
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="insertObj">对象</param>
    /// <returns>自增ID</returns>
    public static int InsertRId<T>(this ISqlSugarClient db, T insertObj) where T : class, new()
    {
        return db.Insertable(insertObj).ExecuteReturnIdentity();
    }

    /// <summary>
    ///     新增并返回自增long ID
    ///     【InsertReturnBigIdentity】
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="insertObj">对象</param>
    /// <returns>自增long ID</returns>
    public static long InsertRbId<T>(this ISqlSugarClient db, T insertObj) where T : class, new()
    {
        return db.Insertable(insertObj).ExecuteReturnBigIdentity();
    }

    /// <summary>
    ///     新增并返回雪花ID
    ///     【InsertReturnSnowflakeId】
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="insertObj">对象</param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <returns>雪花ID</returns>
    public static long InsertRsId<T>(this ISqlSugarClient db, T insertObj, bool isSplitTable = false)
        where T : class, new()
    {
        if (isSplitTable) return db.Insertable(insertObj).SplitTable().ExecuteReturnSnowflakeId();
        return db.Insertable(insertObj).ExecuteReturnSnowflakeId();
    }

    /// <summary>
    ///     新增并返回雪花ID
    ///     【InsertReturnSnowflakeId】
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="insertObjs">集合</param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <returns>雪花ID集合</returns>
    public static List<long> InsertRsId<T>(this ISqlSugarClient db, List<T> insertObjs, bool isSplitTable = false)
        where T : class, new()
    {
        if (isSplitTable) return db.Insertable(insertObjs).SplitTable().ExecuteReturnSnowflakeIdList();
        return db.Insertable(insertObjs).ExecuteReturnSnowflakeIdList();
    }

    #endregion 新增简写扩展

    #region 新增简写扩展-异步

    /// <summary>
    ///     新增并返回自增ID
    ///     【InsertReturnIdentity】
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="insertObj">对象</param>
    /// <returns>自增ID</returns>
    public static async Task<int> InsertRIdAsync<T>(this ISqlSugarClient db, T insertObj) where T : class, new()
    {
        return await db.Insertable(insertObj).ExecuteReturnIdentityAsync();
    }

    /// <summary>
    ///     新增并返回自增long ID
    ///     【InsertReturnBigIdentity】
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="insertObj">对象</param>
    /// <returns>自增long ID</returns>
    public static async Task<long> InsertRbIdAsync<T>(this ISqlSugarClient db, T insertObj) where T : class, new()
    {
        return await db.Insertable(insertObj).ExecuteReturnBigIdentityAsync();
    }


    /// <summary>
    ///     新增并返回雪花ID
    ///     【InsertReturnSnowflakeIdAsync】
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="insertObj">对象</param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <returns>雪花ID</returns>
    public static async Task<long> InsertRsIdAsync<T>(this ISqlSugarClient db, T insertObj, bool isSplitTable = false)
        where T : class, new()
    {
        if (isSplitTable) return await db.Insertable(insertObj).SplitTable().ExecuteReturnSnowflakeIdAsync();
        return await db.Insertable(insertObj).ExecuteReturnSnowflakeIdAsync();
    }

    /// <summary>
    ///     新增并返回雪花ID
    ///     【InsertReturnSnowflakeIdAsync】
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <param name="insertObjs">集合</param>
    /// <param name="isSplitTable">是否分表查询</param>
    /// <returns>雪花ID集合</returns>
    public static async Task<List<long>> InsertRsIdAsync<T>(this ISqlSugarClient db, List<T> insertObjs,
        bool isSplitTable = false) where T : class, new()
    {
        if (isSplitTable) return await db.Insertable(insertObjs).SplitTable().ExecuteReturnSnowflakeIdListAsync();
        return await db.Insertable(insertObjs).ExecuteReturnSnowflakeIdListAsync();
    }

    #endregion 新增简写扩展


    #region 公共扩展方法

    /// <summary>
    ///     List转DataTable
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="list"></param>
    /// <returns></returns>
    public static DataTable ToDataTable<T>(this List<T> list)
    {
        DataTable result = new();
        if (list is not { Count: > 0 }) return result;
        // result.TableName = list[0].GetType().Name; // 表名赋值
        var propertys = list[0].GetType().GetProperties();
        foreach (var pi in propertys)
        {
            var colType = pi.PropertyType;
            if (colType.IsGenericType && colType.GetGenericTypeDefinition() == typeof(Nullable<>))
                colType = colType.GetGenericArguments()[0];
            if (pi.IsIgnoreColumn())
                continue;
            result.Columns.Add(pi.Name, colType);
        }

        foreach (var t in list)
        {
            ArrayList tempList = [];
            foreach (var pi in propertys)
            {
                if (pi.IsIgnoreColumn())
                    continue;
                var obj = pi.GetValue(t, null);
                tempList.Add(obj);
            }

            object[] array = tempList.ToArray();
            result.LoadDataRow(array, true);
        }

        return result;
    }

    /// <summary>
    ///     排除SqlSugar忽略的列
    /// </summary>
    /// <param name="pi"></param>
    /// <returns></returns>
    private static bool IsIgnoreColumn(this PropertyInfo pi)
    {
        var sc = pi.GetCustomAttributes<SugarColumn>(false).FirstOrDefault(u => u.IsIgnore);
        return sc != null;
    }

    /// <summary>
    ///     SqlSugar获取实体信息
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="db"></param>
    /// <returns></returns>
    public static EntityInfo GetEntityInfoByAllDb<T>(this ISqlSugarClient db)
    {
        return SqlSugarHelper.GetEntityInfoByAllDb(typeof(T), db);
    }

    /// <summary>
    ///     SqlSugar获取实体信息
    /// </summary>
    /// <param name="db"></param>
    /// <param name="type"></param>
    /// <returns></returns>
    public static EntityInfo GetEntityInfoByAllDb(this ISqlSugarClient db, Type type)
    {
        return SqlSugarHelper.GetEntityInfoByAllDb(type, db);
    }

    /// <summary>
    ///     转化为线程安全的数据库上下文
    /// </summary>
    /// <param name="db"></param>
    /// <returns></returns>
    public static SqlSugarScope ToScope(this ISqlSugarClient db)
    {
        var scope = new SqlSugarScope(db.CurrentConnectionConfig);
        scope.SetAop();
        return scope;
    }

    /// <summary>
    ///     转化为 SqlSugarClient
    /// </summary>
    /// <param name="db"></param>
    /// <returns></returns>
    public static SqlSugarClient ToClient(this ISqlSugarClient db)
    {
        return db.CopyNew();
    }

    /// <summary>
    ///     线程安全的数据库上下文转化为不安全的数据库上下文
    /// </summary>
    /// <param name="db"></param>
    /// <returns></returns>
    public static SqlSugarClient ToClient(this SqlSugarScope db)
    {
        return db.ScopedContext;
    }

    /// <summary>
    ///     转化为 SqlSugarProvider
    /// </summary>
    /// <param name="db"></param>
    /// <returns></returns>
    public static SqlSugarProvider ToProvider(this SqlSugarClient db)
    {
        return db.Context;
    }

    /// <summary>
    ///     转化为 SqlSugarProvider
    /// </summary>
    /// <param name="db"></param>
    /// <returns></returns>
    public static SqlSugarProvider ToProvider(this SqlSugarScope db)
    {
        return db.ScopedContext.Context;
    }

    #endregion 公共扩展方法
}